//
//  PCDApiGetStorage.m
//  PCDBank
//
//  Created by lyy on 2021/3/17.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiGetStorage.h"
#import "PCDDataBaseModel.h"

#import "PCDLoginSession.h"

@implementation PCDApiGetStorage

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback)responseCallback {
    
    //前端需要session字符串，在获取时候添加不需要改动业务逻辑
    PCDLoginSession *session = [PCDUtil loginSession];
    if (session) {
        NSString *strSession = [session toString];
        [[PCDDataBaseModel shareModel].memoryStorage setValue:strSession forKey:kPCDLoginSessionKey];
    }
    NSDictionary *parameters = (NSDictionary *)data;
    NSString *callBack = [parameters objectForKey:@"success"];
    
    NSString *keyString = [parameters objectForKey:@"key"];
    id memoryDataInfo = [[PCDDataBaseModel shareModel].memoryStorage objectForKey:keyString];
    id dataInfo = nil;
    if (memoryDataInfo) {
        dataInfo = memoryDataInfo;
    } else {
        dataInfo = [[PCDDataBaseModel shareModel] getStorageFromDataBaseWith:parameters];
    }
    
    NSDictionary *callBackDict = @{@"data":dataInfo};
    id dataDict = [dataInfo JSONValue];
    if (dataDict && [dataDict isKindOfClass:[NSDictionary class]]) {
        callBackDict = @{@"data":dataDict};
    }
    
    evaluateJavaScriptOnMainThread(self, callBack, [callBackDict JSONRepresentation])
        
}

@end
